//剩菜
angular.module('SmartCanteenApp.leftover', ['ngRoute', 'ngTouch'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/leftover', {
            templateUrl: 'leftover/leftovers.html',
            controller: 'leftoverCtrl'
        });
        $routeProvider.when('/newLeftover', {
            templateUrl: 'leftover/newLeftover.html',
            controller: 'newLeftoverCtrl'
        });
        $routeProvider.when('/statistics', {
            templateUrl: 'leftover/statistics.html',
            controller: 'statisticsCtrl'
        });
    }])
    
    
	//剩菜管理
    .controller('leftoverCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
   		$scope.hideCanteen()
   		$(".modal-dialog").draggable();//为模态对话框添加拖拽
	    $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
		//分页
		$scope.initial = '10'; //设置默认每页条数
		$scope.pagination([]); //清空容器
		//单日日期选择
		$('#datepicker').datepicker({
		    autoclose: true
		});
		//请求基础数据
		 //请求档口
		$scope.getWins = function(){
		 	$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/windows',{},'get',function(data){
				if(data.code == 0){
					$scope.leftWindows = data.data
				}
			})
		}
		//请求餐别
		$scope.getPeriod_time = function(){
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/period_times',{},'get',function(data){
				if(data.code == 0){
					$scope.leftPeriods = data.data
				}
			})
		}
		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				$scope.getWins()
   				$scope.getPeriod_time()
   		}
		$scope.$on('to-child', function(event,data) {
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
   			$scope.getWins()
   			$scope.getPeriod_time()			
		});
	
		$scope.searchLeftBy = {
			"datetime": $scope.getNowFormatDate(),
			"windows": "0",
			"period_times": "0"
		}
		//查询条件
		$scope.searchLeft = function(){ 
			
			if($scope.searchLeftBy.windows == '0' || $scope.searchLeftBy.period_times == '0'||$scope.searchLeftBy.datetime == ''){
				$scope.showError('日期、档口或餐别不能为空！')
				return
			}
			//回传查询条件 获取数据
			console.log($scope.searchLeftBy)
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/production/canteens/'+$scope.canteenId+'/leftovers',$scope.searchLeftBy,'post',function(data){
				console.log(data)
				if(data.code == 0){
					if(data.data.length == 0){
						$scope.showError('暂无数据')
						$scope.pagination([]);
						return
					}
					$scope.leftItems = data.data
		    		$scope.pagination($scope.leftItems);					
				}
//				else{
//					$scope.reLogin(data.code)
//				}
		})			
		}
		//新增剩菜
		$scope.newLeftOver = function(){
			$scope.jump('/newLeftover')
		}
		//剩菜统计
//		$scope.goStatistics = function(){
//			$scope.jump('/statistics')
//		}
    }])
    
    
    //剩菜登记
    .controller('newLeftoverCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
   		$scope.hideCanteen()
   		$(".modal-dialog").draggable();//为模态对话框添加拖拽
	    $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
		//分页
		$scope.initial = '10'; //设置默认每页条数
		$scope.pagination([]); //清空容器
		//单日日期选择
		$('#datepicker').datepicker({
		    autoclose: true
		});
		//请求基础数据
		//请求档口
		$scope.getWins = function(){
		 	$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/windows',{},'get',function(data){
				console.log(data)
				if(data.code == 0){
					$scope.newleftWindows = data.data
				}
			})
		}
		//请求餐别
		$scope.getPeriod_time = function(){
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/base_system/canteens/'+$scope.canteenId+'/period_times',{},'get',function(data){
				console.log(data)
				if(data.code == 0){
					$scope.newleftPeriods = data.data
				}
			})
		}
		if( $scope.getCanteenId() != ''){
   				$scope.canteenId =  $scope.getCanteenId() ;
   				$scope.getWins()
   				$scope.getPeriod_time()
   		}
		$scope.$on('to-child', function(event,data) {
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
   			$scope.getWins()
   			$scope.getPeriod_time()			
		});
		//查询条件
		var nowDate = $scope.getNowFormatDate().concat()
		$scope.searchNewLeftoverBy = {
			"datetime": nowDate,
			"windows": "0",
			"period_times": "0",
		}
		$scope.searchNewLeftover = function(){
			console.log($scope.searchNewLeftoverBy)
			if($scope.searchNewLeftoverBy.datetime == '' || $scope.searchNewLeftoverBy.windows == '0' || $scope.searchNewLeftoverBy.period_times == '0'){
				$scope.showError('日期、档口或餐别不能为空！')
				return
			}
			//发送条件查询
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/production/canteens/'+$scope.canteenId+'/leftovers',$scope.searchNewLeftoverBy,'post',function(data){
				console.log(data)
				$scope.newLeftOver = data.data
				if(data.code == 0){
					$scope.leftOverDate = $scope.searchNewLeftoverBy.datetime
					if(data.data.length == 0){
						$scope.showError('暂无数据')
					}
				}else{
					$scope.showError(data.msg)
				}
			})
		}
		$scope.passVlidate = true
		//剩菜数量小于计划数量验证
		$scope.vlidateNum = function(register_number,planned_number){
			if(register_number>planned_number){
				$scope.passVlidate = false
				$scope.showError('登记数量不能大于计划数量')
			}
		}
		//提交修改
		$scope.submitLeftover = function(){
			$scope.subData = {
				"datetime": $scope.leftOverDate,
				"windows": $scope.searchNewLeftoverBy.windows,
				"period_times": $scope.searchNewLeftoverBy.period_times,
				"items": []
			}
			
			$scope.subData.items = $scope.newLeftOver
			if($scope.leftOverDate == undefined){
				$scope.showError('请先进行查询')
				return
			}
			if(!$scope.passVlidate){
				$scope.showError('登记数量不能大于计划数量')
				return
			}
			$scope.keepGoSubmitLeftover = true
			//剩菜数量不能为空
			angular.forEach($scope.subData.items,function(data){
//				console.log(data.register_number)
				if(data.register_number == null || data.register_number == ''){
					$scope.showError('剩菜不能为空')
					$scope.keepGoSubmitLeftover = false
				}
            });
            if(!$scope.keepGoSubmitLeftover){
            	return
            }
			$('#alertDate').modal('toggle')
			console.log($scope.subData)
		}
		$scope.sureSubmitLeftover = function(){
			$('#alertDate').modal('toggle')
			$scope.getJson($scope.resultPath+'/wisdomeatery/v1/production/canteens/'+$scope.canteenId+'/leftovers',$scope.subData,'put',function(data){
				console.log(data)
				if(data.code == 0){
					$scope.showSuccess()
				}else{
//					$scope.reLogin(data.code)
					$scope.showError()
				}
			})
		}
    }])
    
    
    
//  //剩菜统计
//  .controller('statisticsCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
// 		$(".modal-dialog").draggable();//为模态对话框添加拖拽
//	    $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
//		//分页
//		$scope.initial = '10'; //设置默认每页条数
//		$scope.pagination([]); //清空容器
//		//单日日期选择
//		$('#datepicker').datepicker({
//		    autoclose: true
//		});
//
//		//查询条件
//		$scope.searchNewLeftoverBy = {
//			"datetime": $scope.getNowFormatDate(),
//			"windows": "1",
//			"period_times": "1",
//			"item_name":""
//		}
//		$scope.searchNewLeftover = function(){
//			console.log($scope.getId) //食堂id
//			console.log($scope.searchNewLeftoverBy)
//		}
//  }])
  